﻿function SWFLoad1(swfu, loadSettings, dbHelperString) {
    var bb;
    swfu.SWFObj = new SWFUpload1({
            upload_url: applicationPath + "/UserControls/AttachmentUpload.aspx?AttachmentDBHelperString=" + dbHelperString,
            file_size_limit: loadSettings.file_size_limit,
            file_types: loadSettings.file_types,
            file_types_description: loadSettings.file_types_description,
            file_upload_limit: loadSettings.file_upload_limit,

            file_queue_error_handler: fileQueueError1,
            file_dialog_complete_handler: fileDialogComplete1,
            upload_progress_handler: uploadProgress1,
            upload_error_handler: uploadError1,
            upload_success_handler: loadSettings.upload_success_handler,
            upload_complete_handler: uploadComplete1,

            button_action: loadSettings.button_action,
            button_cursor: SWFUpload1.CURSOR.HAND,
            button_disabled: loadSettings.button_disabled,
            button_image_url: applicationPath + "/Images/Attachment/uploadBtn_" + currentCulture + ".png",
            button_placeholder_id: loadSettings.button_placeholder_id,
            button_width: 62,
            button_height: 22,
            button_text_top_padding: 1,
            button_text_left_padding: 5,

            flash_url: applicationPath + '/Images/Attachment/swfupload.swf',    //应用程序路径+swf路径

            custom_settings: loadSettings.custom_settings,
            debug: false
    });
}

function uploadError1(file, errorCode, message) {
    var imageName = "error.gif";
    var progress;
    try {
        switch (errorCode) {
            case SWFUpload1.UPLOAD_ERROR.FILE_CANCELLED:
                try {
                    progress = GetFileProgressObject1(this.customSettings, file, this.customSettings.upload_target);
                    progress.setCancelled();
                    progress.setStatus("Cancelled");
                    progress.toggleCancel(false);
                }
                catch (ex1) {
                    this.debug(ex1);
                }
                break;
            case SWFUpload1.UPLOAD_ERROR.UPLOAD_STOPPED:
                try {
                    progress = GetFileProgressObject1(this.customSettings, file, this.customSettings.upload_target);
                    progress.setCancelled();
                    progress.setStatus("Stopped");
                    progress.toggleCancel(true);
                }
                catch (ex2) {
                    this.debug(ex2);
                }
            default:
                alert(message);
                break;
        }
    } catch (ex3) {
        this.debug(ex3);
    }

}

function fileDialogComplete1(numFilesSelected, numFilesQueued) {
    try {
        if (numFilesQueued > 0) {
            this.startUpload();
            //禁用提交按钮
            document.getElementById(this.customSettings.submitBtnId).disabled = true;
        }
    } catch (ex) {
        this.debug(ex);
    }
}

/*为异步请求数据提供支持   结束*/
function fileQueueError1(file, errorCode, message) {
    try {
        var errorMsg = "";
        switch (errorCode) {
            case SWFUpload1.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
                if (message > 0) {
                    errorMsg = "Only can upload " + message + " files.";
                } else {
                    errorMsg = "Continue uploading files is forbidden.";
                }
                break;
            case SWFUpload1.QUEUE_ERROR.ZERO_BYTE_FILE:
                errorMsg = resAlertZeroFile;
                break;
            case SWFUpload1.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                errorMsg = resAlertBigFile + this.settings.file_size_limit;
                break;
            case SWFUpload1.QUEUE_ERROR.ZERO_BYTE_FILE:
            case SWFUpload1.QUEUE_ERROR.INVALID_FILETYPE:
            default:
                alert(message);
                break;
        }
        if (errorMsg !== "") {
            alert(errorMsg);
            return;
        }
        //addImage("images/" + imageName);
    } catch (ex) {
        this.debug(ex);
    }

}

function uploadProgress1(file, bytesLoaded) {

    try {
        var percent = Math.ceil((bytesLoaded / file.size) * 100);

        var progress = GetFileProgressObject1(this.customSettings, file, this.customSettings.upload_target);
        progress.setProgress(percent);
        if (percent === 100) {
            progress.toggleCancel(false, this);
        } else {
            progress.toggleCancel(true, this);
        }
    } catch (ex) {
        this.debug(ex);
    }
}

//全部文件上传完毕
function uploadComplete1(file) {
    try {
        if (this.getStats().files_queued > 0) {
            this.startUpload();
        } else {
            //启用提交按钮
            document.getElementById(this.customSettings.submitBtnId).disabled = false;
        }
    } catch (ex) {
        this.debug(ex);
    }
}

//定义常用属性
var CustomSettingsInfo =
{
    //可预览的图片类型
    imgType: new Array(".jpg", ".gif", ".png", ".bmp", ".jpeg"),
    //判断是否为可预览的图片类型
    isImg: function(currImgType) {
        currImgType = currImgType.toLowerCase();
        for (var i = 0; i < this.imgType.length; i++) {
            if (this.imgType[i] == currImgType) {
                return true;
            }
        }
        return false;
    }
};


//获取FileProgress对象
function GetFileProgressObject1(cSettings, file, targetID) {
    return new FileProgress1(file, targetID);
}


/* ******************************************
*	控制对象显示HTML信息
* ****************************************** */
function FileProgress1(file, targetID) {
    this.fileProgressID = file.id;
    this.fileProgressWrapper = document.getElementById(this.fileProgressID);
    if (!this.fileProgressWrapper) {
        this.fileProgressWrapper = document.createElement("tr");
        this.fileProgressWrapper.className = "AT-Data";
        this.fileProgressWrapper.id = this.fileProgressID;

        //文件名
        var fileNameTD = document.createElement("td");
        fileNameTD.className = "AT-Data-FileName";

        var download = document.createElement("a");
        download.className = "AT-Data-FileNameLink";
        download.appendChild(document.createTextNode(file.name));
        fileNameTD.appendChild(download);
        
        //文件大小
        var fileSizeTD = document.createElement("td");
        fileSizeTD.className = "AT-Data-FileSize";
        fileSizeTD.appendChild(document.createTextNode(GetAttachmentSize(file.size)));

        //上传日期
        var fileDateTD = document.createElement("td");
        fileDateTD.className = "AT-Data-FileDate";

        //上传状态
        var fileUploadStateTD = document.createElement("td");
        fileUploadStateTD.className = "AT-Data-FileUploadStatus";

        var progressStatus = document.createElement("div");
        progressStatus.className = "AT-Data-FileUploadDescription";
        var progressBar = document.createElement("div");
        progressBar.className = "AT-Data-FileUploadProcessBar";
        fileUploadStateTD.appendChild(progressStatus);
        fileUploadStateTD.appendChild(progressBar);
        
        //添加节点
        this.fileProgressWrapper.appendChild(fileNameTD);
        this.fileProgressWrapper.appendChild(fileSizeTD);
        this.fileProgressWrapper.appendChild(fileDateTD);
        this.fileProgressWrapper.appendChild(fileUploadStateTD);

        document.getElementById(targetID).childNodes[0].appendChild(this.fileProgressWrapper);
    } else {
    }
    this.height = this.fileProgressWrapper.offsetHeight;
}


FileProgress1.prototype.setProgress = function(percentage) {
    //this.fileProgressWrapper.className = "SWFUpload1_progressContainer SWFUpload1_blue";
    //this.fileProgressWrapper.childNodes[3].childNodes[1].className = "SWFUpload1_progressBarInProgress";
    this.fileProgressWrapper.childNodes[3].childNodes[1].style.width = percentage + "%";
    this.fileProgressWrapper.childNodes[3].childNodes[0].innerHTML = resUploadPercentage + percentage + "%";
};

FileProgress1.prototype.setComplete = function() {
    //this.fileProgressWrapper.className = "SWFUpload1_progressContainer green";
    //this.fileProgressWrapper.childNodes[3].childNodes[0].className = "SWFUpload1_progressBarComplete";
    this.fileProgressWrapper.childNodes[3].childNodes[0].style.width = "";
};

FileProgress1.prototype.setError = function() {
    //this.fileProgressWrapper.className = "SWFUpload1_progressContainer SWFUpload1_red";
    //this.fileProgressWrapper.childNodes[3].childNodes[0].className = "SWFUpload1_progressBarError";
    this.fileProgressWrapper.childNodes[3].childNodes[0].style.width = "";
};

FileProgress1.prototype.setCancelled = function() {
    //this.fileProgressWrapper.className = "SWFUpload1_progressContainer";
    //this.fileProgressWrapper.childNodes[3].childNodes[0].className = "SWFUpload1_progressBarError";
    this.fileProgressWrapper.childNodes[3].childNodes[0].style.width = "";
};

FileProgress1.prototype.setStatus = function(status) {
    this.fileProgressWrapper.childNodes[3].childNodes[0].innerHTML = status;
};

FileProgress1.prototype.toggleCancel = function(show, SWFUpload1Instance) {
};

function AddAttachment1(AttachmentID, AttachmentName, AttachmentSize, AttachmentUploadTime, deletelistID, AttachmentTableID, AttachmentDBHelperString) {

    this.fileProgressWrapper = document.createElement("tr");
    this.fileProgressWrapper.className = "AT-Data";

    //文件名
    var fileNameTD = document.createElement("td");
    fileNameTD.className = "AT-Data-FileName";
    var download = document.createElement("a");
    download.className = "AT-Data-FileNameLink";
    download.href = applicationPath + "/UserControls/AttachmentDownload1.aspx?AttachmentID=" + AttachmentID + "&AttachmentDBHelperString=" + AttachmentDBHelperString;
    download.appendChild(document.createTextNode(AttachmentName));
    fileNameTD.appendChild(download);
    
    //文件大小
    var fileSizeTD = document.createElement("td");
    fileSizeTD.className = "AT-Data-FileSize";
    fileSizeTD.appendChild(document.createTextNode(GetAttachmentSize(AttachmentSize)));

    //上传日期
    var fileDateTD = document.createElement("td");
    fileDateTD.className = "AT-Data-FileDate";
    fileDateTD.appendChild(document.createTextNode(AttachmentUploadTime));

    //上传状态
    var fileUploadStateTD = document.createElement("td");
    fileUploadStateTD.className = "AT-Data-FileUploadStatus";
    var progressStatus = document.createElement("div");
    progressStatus.className = "AT-Data-FileUploadDescription";
    progressStatus.innerHTML = resUploadSuccess;
    fileUploadStateTD.appendChild(document.createTextNode("已上传"));
   
    //添加节点
    this.fileProgressWrapper.appendChild(fileNameTD);
    this.fileProgressWrapper.appendChild(fileSizeTD);
    this.fileProgressWrapper.appendChild(fileDateTD);
    this.fileProgressWrapper.appendChild(fileUploadStateTD);

   var attobj =  document.getElementById(AttachmentTableID);
   if (attobj != null) {
   attobj.childNodes[0].appendChild(this.fileProgressWrapper);
   }
}

function GetAttachmentSize(sizeBytes) {
    var sizeDes;
    //mb级别的
    if (sizeBytes / 1048576 > 1) {
        sizeDes = (sizeBytes / 1048576).toFixed(2) + ' MB';
    }
    //kb级别
    else {
        sizeDes = (sizeBytes / 1024).toFixed(2) + ' KB';
    }
    return sizeDes;
}

function uploadSuccess1(file, serverData, swfObj, AttachmentDBHelperString) {
    try {
        var progress = GetFileProgressObject1(swfObj.customSettings, file, swfObj.customSettings.upload_target);

        var addlistID = swfObj.customSettings.addIDList;
        var deletelistID = swfObj.customSettings.deleteIDList;

        var dataList = JSON.parse(serverData);
        var fileInfo = "";
        if (dataList.length > 0) {
            fileInfo = dataList[dataList.length - 1];

            //add by 崔茂山 2010年12月6日23:42:27 在上传成功后，加入下载的链接
            var thisFileWrapper = document.getElementById(file.id);

            thisFileWrapper.AttachmentID = fileInfo.AttachmentID;

            thisFileWrapper.childNodes[0].childNodes[0].href = applicationPath + "/UserControls/AttachmentDownload1.aspx?AttachmentID=" + fileInfo.AttachmentID + "&AttachmentDBHelperString=" + AttachmentDBHelperString;
            thisFileWrapper.childNodes[2].innerText = fileInfo.UploadTime;

//            var btnDelete = thisFileWrapper.childNodes[4].childNodes[0];
//            btnDelete.src = applicationPath + "/Images/Grid/GridDelete.gif",
//            btnDelete.disabled = "";
//            btnDelete.onclick = function() {
//                DeleteAttachment(fileInfo.AttachmentID, deletelistID, btnDelete);
//            };

            //将attachmentID加入到addlistID隐藏控件中，用逗号分开
            var hidAddlist = document.getElementById(addlistID);
            hidAddlist.value += "'" + fileInfo.AttachmentID + "',";

        }
        //上传完成去掉进度条
        removeElement(thisFileWrapper.childNodes[3].childNodes[1]);
        progress.setStatus(resUploadSuccess);
        progress.toggleCancel(false);

    } catch (ex) {
        this.debug(ex);
    }
}